package com.vzoom.logdesensitization.strategyimpl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vzoom.logdesensitization.DesensitizationStrategy;
import com.vzoom.logdesensitization.StringConstant;
import com.vzoom.logdesensitization.util.ObjectMappers;
import org.springframework.beans.factory.InitializingBean;

public class JsonDesensitizationStrategy implements DesensitizationStrategy, InitializingBean {

    private static ObjectMapper objectMapper;

    @Override
    public String desensitization(Object origin) {
        if (origin == null) {
            return StringConstant.NULL;
        }
        try {
            return objectMapper.writeValueAsString(origin);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        objectMapper = ObjectMappers.getJsonObjectMapper();
    }
}
